<?php
	class user extends common{
		private $table_user='user';				//用户表
		private $table_admin='admin';			//小编表
		private $table_user_fans='user_fans';	//用户关注表
		private $table_user_work_experience='user_work_experience';			//工作经历表
		private $table_user_oversea_experience='user_oversea_experience';	//海外经历表
		private $table_user_educate_experience='user_educate_experience';	//教育经历表

		private $province="pcc_province";		//省
		private $city="pcc_city";				//市
		private $county='pcc_county';			//区
		private $continent='continent';			//洲
		private $country='country';				//国家

		var $infoPercent=array(
			'account'=>10,'email'=>10,'phone'=>10
			,'sex'=>10,'real_name'=>10,'self_intro'=>10
			,'address'=>10,'avatar'=>10,'workList'=>15
			,'educateList'=>15,'overseaList'=>20,'total'=>130
			);																//会员信息百分比

		function __construct(){
			parent::__construct();
		}

		/**
		 * 关注数量     
		 * @author jsy 
		 * @param int $user_id
		 * @param string $where
		 * @return int         
		 */
		function user_fans_num($where){
			$sql=$this->table_user_fans.' c where 1=1 '.$where;
			$num=$this->getNum($sql);
			return $num;
		}

		/**
		 * 粉丝关注查询
		 * @param  string  $where 
		 * @param  string  $key   
		 * @param  integer $num   
		 * @return array 
		 * @author wxy        
		 */
		function userFans($where='',$key='c.*',$num=0){
			$sql=$this->table_user_fans.' c where 1=1 '.$where;
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		/**
		 * 关注列表    
		 * @author jsy 
		 * @param  int  $id
		 * @return array         
		 */
		function user_attention($id,$where=''){
			$where.=' order by f.id asc';
			$joinTable='';
			$sql=$this->table_user_fans." f WHERE 1=1 ".$where;
			$key='f.*';
			$list=$this->getList($sql,$key,$num=0);
			//前天会员、后台小编id
			$admin_ids='';
			$user_id='';
			if(is_array($list['result'])){
				foreach ($list['result'] as $k => $v) {
					$list['result'][$k]['gz']=$this->user_fans_num(" AND c.fans_id=".$v['user_id']);
					$list['result'][$k]['bgz']=$this->user_fans_num(" AND c.user_id=".$v['user_id'].' AND c.type="'.$v['type'].'"');
					if($v['type']=='user'){
						$user_ids.=$v['user_id'].',';
					}

					if($v['type']=='admin'){
						$admin_ids.=$v['user_id'].',';
					}
				}
			}
			$user_ids=$user_ids==''?0:substr($user_ids,0,-1);
			$admin_ids=$admin_ids==''?0:substr($admin_ids,0,-1);
			$where=' AND a.id in('.$user_ids.')';
			$users=$this->userList('user',$where);			//会员信息
			$where=' AND a.id in('.$admin_ids.')';
			$key='a.*,a.name as account,a.intro as self_intro';
			$admins=$this->userList('admin',$where,$key);	//小编信息
			//信息组合
			if(is_array($list['result'])){
				foreach ($list['result'] as $k => $v) {
					if(is_array($users['result'])){
						foreach ($users['result'] as $key => $val) {
							if($v['type']=='user'&&$v['user_id']==$val['id']){
								$list['result'][$k]['account']=$val['account'];
								$list['result'][$k]['avatar']=$val['avatar'];
								$list['result'][$k]['self_intro']=$val['self_intro'];
							}
						}
					}
					if(is_array($admins['result'])){
						foreach ($admins['result'] as $key => $val) {
							if($v['type']=='admin'&&$v['user_id']==$val['id']){
								$list['result'][$k]['account']=$val['account'];
								$list['result'][$k]['avatar']=$val['avatar'];
								$list['result'][$k]['self_intro']=$val['self_intro'];
							}
						}
					}
				}
			}
			return $list;
		}

		/**
		 * 会员列表
		 * @param  string  $type  
		 * @param  string  $where 
		 * @param  string  $key   
		 * @param  integer $num   
		 * @return array
		 * @author wxy         
		 */
		function userList($type='user',$where='',$key="a.*",$num=0){
			$table=$type=='user'?$this->table_user:$this->table_admin;
			$sql=$table." a WHERE 1=1 ".$where;
			$key.='';
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		/**
		 * 获得用户信息
		 * @author jsy 
		 * @param int $id
		 * @param string $key
		 * @return result
		 */
		function userInfo($id, $key='*'){
			if($id){
				$sql=$this->table_user.' i where i.id="'.$id.'"';
				//$key.=',(select city from '.$this->city.' where cityid=i.city) as cityName';
				$info=$this->getInfo($sql,$key);	
			}	
			return $info;
		}

		/**
		 * 会员工作经历
		 * @param  integer  $user_id 
		 * @param  string  $key     
		 * @param  integer $num     
		 * @return array
		 * @author wxy           
		 */
		function userWorkList($user_id,$key='*',$num=0){
			$sql=$this->table_user_work_experience.' where user_id="'.$user_id.'"';
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		/**
		 * 会员教育经历
		 * @param  integer  $user_id 
		 * @param  string  $key     
		 * @param  integer $num     
		 * @return array
		 * @author wxy          
		 */
		function userEducateList($user_id,$key='*',$num=0){
			$sql=$this->table_user_educate_experience.' where user_id="'.$user_id.'"';
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		/**
		 * 会员海外经历
		 * @param  integer  $user_id 
		 * @param  string  $key     
		 * @param  integer $num     
		 * @return array
		 * @author wxy           
		 */
		function userOverseaList($user_id,$key='e.*',$num=0){
			$key.=',(select continent from '.$this->continent.' where e.continent_id=id) as continent,
					(select country from '.$this->country.' where e.country_id=id ) as country
					,from_unixtime(e.entrance_time,"%Y-%m-%d") as entranceTime
					,from_unixtime(e.granduate_time,"%Y-%m-%d") as granduateTime';
			$sql=$this->table_user_oversea_experience.' e where e.user_id="'.$user_id.'"';
			$list=$this->getList($sql,$key,$num);
			return $list;
		}

		function continentList($where=''){
			$sql=$this->continent.' where 1=1'.$where;
			$list=$this->getList($sql);
			return $list;
		}

		function countryList($where=''){
			$sql=$this->country.' where 1=1'.$where;
			$list=$this->getList($sql);
			return $list;
		}

		/**
		 * 更新会员信息
		 * @param  array $arr 
		 * @param  string $where
		 * @return bool
		 * @author wxy      
		 */
		function userInfoUpdate($arr,$where){
			$res=$this->update($this->table_user,$arr,$where);
			return $res;
		}

		/**
		 * 删除会员经历（工作，海外，教育）
		 * @param  string $label 		经历类型；    
		 * @param  int $id        
		 * @param  string $searchKey 
		 * @return bool           
		 * @author wxy
		 */
		function userExperienceDel($label='work',$id,$searchKey="id"){
			if($label=='work'){
				$table=$this->table_user_work_experience;
			}
			if($label=='educate'){
				$table=$this->table_user_educate_experience;
			}
			if($label=='oversea'){
				$table=$this->table_user_oversea_experience;
			}
			$id=$id!=''?$id.',':'0,';
			$res=$this->del($table,$id,'',$searchKey);
			return $res;
		}

		/**
		 * 新增会员经历（工作。教育，海外）
		 * @param  string $label 
		 * @param  array $arr   
		 * @return bool  
		 * @author wxy      
		 */
		function userExperienceInsert($label,$arr){
			if($label=='work'){
				$table=$this->table_user_work_experience;
			}
			if($label=='educate'){
				$table=$this->table_user_educate_experience;
			}
			if($label=='oversea'){
				$table=$this->table_user_oversea_experience;
			}
			$res=$this->insert($table,$arr);
			$id=$this->insert_id();
			return $id;
		}

		/**
		 * 注册验证
		 * @param $user
		 */
		function validator($user){
			$key = array('email'=>'邮箱','name'=>'用户名', 'pwd'=>'密码', 'pwd2'=>'重复密码'
					,'sex'=>'性别', 'city'=>'所在城市', 'imgcode'=>'验证码');
			$error="0";
			foreach($user as $k=>$v){
				$len=strlen(trim($v));
				if($len=='0' && $k!='county'){
					$error="1";
					$msg=$k;
					break;
				}
			}
			
			if($error=='1'){
				$msg=$key[$msg]."不能为空";
			}elseif($user['pwd']!=$user['pwd2']){
				$error='1';
				$msg='两次密码不相同';
			}elseif(strtolower($user['imgcode'])!=strtolower($_SESSION['codeimg'])){
				$error='1';
				$msg='验证码错误';
			}else{
				$emailNum=common::count("user","email='".$user['email']."'");
				if($emailNum>='1'){
					$error='0';
					$msg="邮箱已被占用，请重新输入";
				}
			}
			
			$result=array('error'=>$error,'msg'=>$msg);
			return $result;
		}

		/**
		 * 获得网站的smtp服务
		 * @return array
		 */
		function getSmtp(){
			$sql='setting where class="email" ';
			$list=$this->getList($sql);
			return $list['result'];
		}
		
		/**
		 * 注册
		 * @param $user
		 * @author wxy
		 */
		function register($user){
			$this->insert('user',$user);
			$user_id=$this->insert_id();
			return $user_id;
		}
		
		/**
		 * 登录
		 * @param $email
		 * @param $pwd
		 */
		function login($email,$pwd){
			$where="email='".$email."' and pwd='".md5($pwd)."'";
			$count=common::getNum("user where ".$where);
			if($count<=0){
				$error='1';
				$msg='用户不存在';
			}else{
				$userInfo=common::getInfo('user',$where,"id,pwd");
				if($userInfo['pwd']!=md5($pwd)){
					$error='1';
					$msg="密码错误";
				}else{
					$error="0";
					$msg="登录成功";
					$user_id=$userInfo['id'];
				}
			}
			
			$result=array('error'=>$error,'msg'=>$msg,'user_id'=>$user_id);
			return $result;
		}
		
		/**
		 * 获得用户信息
		 * @param unknown_type $id
		 * @param unknown_type $key
		 * @return result
		 */
	/*	function userInfo($id, $key='*'){
			$sql="select ".$key." 
				  from user u
				  left join pcc_province pp on pp.provinceid=u.province
				  left join pcc_city pc on pc.cityid=u.city
				  left join pcc_county pcc on pcc.countyid=u.county  
				  where u.id='".$id."'";
			$query=$this->query($sql);
			$user=$this->fetch_array($query);
			return $user;
		}*/

		/**
		 * 用户头像
		 * @param $user_id
		 * @param $type
		 */
		function userAvatar($user_id,$type="middle"){
			$sql="select avatar from `user` where id='".$user_id."'";
			$query=$this->query($sql);
			$row=$this->fetch_array($query);
			
			if($row['avatar']==false){
				$avatar = "images/noavatar_".$type.".gif";
			}else{
				$avatar = "upload/avatar/".$type."/".$row['avatar'].'.jpg';
			}
			
			return $avatar;
		}
	}
?>